(function() {
    'use strict';

    const   childProcess = require('child_process'),
            util         = require('util'),
            events       = require('events');

    class Image2Muxer extends events.EventEmitter {

        constructor(options) {
            super();

            this.url = options.url;
            console.log("url is "+this.url);
            this.stream = childProcess.spawn('ffmpeg', [
            '-loglevel', 'quiet'
            , '-i', this.url
            , '-r', '6'
            //, '-vf', 'fps=25'
            , '-q:v', '10'
            //, '-b:v', '32k'
            , '-f', 'image2'
            , '-s', '320x240'
            , '-updatefirst', '1'
            , '-'
            ]);
        
            //this.pid = this.stream.pid;
            //this.inputStreamStarted = true;
            this.stream.stdout.on('data', (data) => {
                //let frame = new Buffer(data).toString('base64');
                return this.emit('image2data', data);
        });
        this.stream.stderr.on('data', (data) => {
            return this.emit('image2error', data);
        });

    }
    closeMuxer() {
        if (this.stream != null) {
            //this.stream.kill('SIGINT');
            this.stream.stop();
        }
    }
};

module.exports = Image2Muxer;
}());
